package com.tom.joke;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

/**
 * QQ 聊天通讯。
 */
public class MessageService extends Service {
    private static final String TAG = "MessageService";
    private MessageBind mMessageBind;

    @Override
    public void onCreate() {
        super.onCreate();

        new Thread(new Runnable() { // 模拟一直接收消息。
            @Override
            public void run() {
                while (true) {
                    Log.e(TAG, "run: 等待接收消息 ");
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                    }
                }
            }
        }).start();

        if (mMessageBind == null) {
            mMessageBind = new MessageBind();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 创建通知频道
            NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
        // 创建通知并设置前台服务
        Notification notification = new NotificationCompat.Builder(this, "channel_id")
                .setContentTitle("My Service")
                .setContentText("Service is running in foreground")
                .build();
        // **提高进程的优先级, 提高到前台进程(可选)**
        startForeground(100, notification);
        // 绑定建立连接
        bindService(new Intent(this, GuardService.class), mServiceConn, Context.BIND_IMPORTANT);
        return Service.START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
//        return mMessageBind;
        return new ProcessConnection.Stub() {};
    }

    private class MessageBind extends ProcessConnection.Stub {

    }

    private ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // service 连接上
            Log.d("TAG", "MessageService -- onServiceConnected");
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("TAG", "MessageService -- onServiceDisconnected");
            // 断开连接, 重新启动，重新绑定
            startService(new Intent(MessageService.this, GuardService.class));
            bindService(new Intent(MessageService.this, GuardService.class),
                    mServiceConn, Context.BIND_IMPORTANT);
        }
    };

    // onBinder return mBinder;
    private final UserAidl.Stub mBinder = new UserAidl.Stub() {
        @Override
        public String getUserName() throws RemoteException {
            return "abc@163.com";
        }

        @Override
        public String getUserPwd() throws RemoteException {
            return "123456";
        }
    };

}
